package com.xbdun.service.bmp.facade;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xbdun.common.api.Client;
import com.xbdun.common.enums.XbdunPkgEnum;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.request.system.SysUserPagerRequest;
import com.xbdun.facade.bmp.request.system.SysUserRequest;
import com.xbdun.facade.bmp.result.system.QuerySysUsersPagerResult;
import com.xbdun.facade.bmp.result.system.SysUserResult;
import com.xbdun.facade.bmp.service.system.SysUserFacade;
import com.xbdun.service.bmp.service.system.SysUserService;

import lombok.extern.slf4j.Slf4j;

@Component
//@Service(group = DubboConstant.BUSI_PROTAL_GROUP, version = DubboConstant.BUSI_PROTAL_VERSION,retries=DubboConstant.BUSI_PROTAL_RETRY)
@Slf4j
public class SysUserFacadeImpl implements SysUserFacade {

	@Autowired
	SysUserService sysUserService;
	public SysUserResult getSysUserLogin(SysUserRequest request) {
		SysUserResult result = new SysUserResult();
		result.setBizException(BizException.DB_SELECTONE_IS_NULL.newInstance("用户不存在"));
		try {
			log.info("start - 获取平台用户信息,入参,{}", request);
			checkedGetSysUser(request);
			result.setData(sysUserService.getSysUserLogin(request));
			if(null!=result.getData()){
				result.getData().setUnionid(StringUtils.EMPTY);
				result.setBizException(BizException.SUCCESS);
			}
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取平台用户信息失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 获取平台用户信息结果:{}", result);
		}
		return result;
	}
	
	//检测用户
	private void checkedGetSysUser(SysUserRequest request) {
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isBlank(request.getUnionid())){
			sb.append("用户的微信UnionId不能为空;");
		}
		XbdunPkgEnum enum1 = XbdunPkgEnum.getEnum(request.getPkgName());
		if(null == enum1){
			sb.append("系统包登录失败，系统未定义;");
		}
		if(StringUtils.isNotBlank(request.getUserCode())&&!NumberUtils.isDigits(request.getUserCode())){
			sb.append("用户编号不合法;");
		}
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}

	public QuerySysUsersPagerResult queryUserPager(SysUserPagerRequest request, Client client) {
		QuerySysUsersPagerResult result = new QuerySysUsersPagerResult();
		try {
			log.info("start - 检索用户入参,{}", request);
			
			CommonUtil.validateClient(client);
			if(StringUtils.isNotBlank(request.getIsAdd())){
				//为增加查询且为管理员,查询平台表
				if(request.getIsAdd().equals("true")&&client.getRoleId()!=1){
					result.setData(sysUserService.querySysUserPager(request,client));
				}
				if(request.getIsAdd().equals("true")&&client.getRoleId()==1){
					throw BizException.POWER_FAIL.newInstance("对不起您没有权限新增用户;");
				}
				//不为新增并且为管理员,查询机构＋平台表
				if (request.getIsAdd().equals("")){
					result.setData(sysUserService.queryOrgUserPager(request,client));
				}
			}else{
				throw BizException.PARAM_CHKE_FAIL.newInstance("未指定是否为操作类型;");
			}
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("检索用户失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 检索用户结果:{}", result);
		}
		return result;
	}
}
